package com.woniuxy.school.online.repair.util;

import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.woniuxy.school.online.repair.dto.GaodeNavigationDto;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 导航功能工具类
 */
public class GaodeNavigationUtil {
    private static final String API_KEY = "ca2b186435e126c21cb690b58140e300";

    public static void main(String[] args) {
        String origin = "116.481028,39.989643";  // 起点坐标
        String destination = "116.465302,40.004717";  // 终点坐标

        try {
            String apiUrl = buildRequestUrl(origin, destination);
            String response = getRoute(apiUrl);
            GaodeNavigationDto gaodeNavigationDto = parseRouteData(response);
            System.out.println(gaodeNavigationDto);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取导航结果
     * @param origin 起点坐标
     * @param destination 终点坐标
     * @return
     */
    public static GaodeNavigationDto getNavigationResult(String origin,String destination) {
        try {
            String apiUrl = buildRequestUrl(origin, destination);
            String response = getRoute(apiUrl);
            GaodeNavigationDto gaodeNavigationDto = parseRouteData(response);
            gaodeNavigationDto.setPolyline(origin+";"+destination);
            System.out.println(gaodeNavigationDto);
            return gaodeNavigationDto;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    // 构建请求URL
    private static String buildRequestUrl(String origin, String destination) {
        return "https://restapi.amap.com/v3/direction/driving"
                + "?key=" + API_KEY
                + "&origin=" + origin
                + "&destination=" + destination
                + "&strategy=0";  // 0-速度优先，1-费用优先
    }

    // 发送请求并获取响应
    private static String getRoute(String apiUrl) throws IOException {
        try (CloseableHttpClient client = HttpClients.createDefault()) {
            HttpGet request = new HttpGet(apiUrl);
            return EntityUtils.toString(client.execute(request).getEntity());
        }
    }

    // 解析导航数据
    private static GaodeNavigationDto parseRouteData(String response) {
        GaodeNavigationDto dto = new GaodeNavigationDto();
        JSONObject json = JSONUtil.parseObj(response);
        JSONObject route = json.getJSONObject("route");
        JSONArray paths = route.getJSONArray("paths");
        if (paths.size() > 0) {
            JSONObject path = paths.getJSONObject(0);
            System.out.println("总距离：" + path.getStr("distance") + "米");
            System.out.println("预计耗时：" + path.getStr("duration") + "秒");
            dto.setDistance("总距离：" + path.getStr("distance") + "米");
            dto.setDuration("预计耗时：" + path.getStr("duration") + "秒");

            //保存总共需要的步骤
            List<String> procedures = new ArrayList<>();
            JSONArray steps = path.getJSONArray("steps");
            for (int i = 0; i < steps.size(); i++) {
                JSONObject step = steps.getJSONObject(i);
                System.out.println("步骤" + (i + 1) + ": " + step.getStr("instruction"));
                procedures.add(i,"步骤" + (i + 1) + ": " + step.getStr("instruction"));
            }
            dto.setSteps(procedures);
        }
        return dto;
    }
}

